# Archlive <http://archlive-pkg.googlecode.com>

pkgname=vmware-workstation
_basever=7.1.4
_build=385536
pkgver="${_basever}_${_build}"
pkgrel=1
arch=('i686' 'x86_64')
license=('BUSINESS')
pkgdesc="VMware kernel modules"
URL="http://www.vmware.com/"
option=(!purge)

_knver=$(uname -r)
VM_INSTALL_DIR="/opt/vmware"

# You must download VMware...bundle by yourselves
if [ "${CARCH}" = 'x86_64' ]; then
	#A="VMware-Workstation-${_basever}-${_build}.x86_64.bundle"
	A="VMware-Workstation-Full-${_basever}-${_build}.x86_64.bundle"
else
	#A="VMware-Workstation-${_basever}-${_build}.i386.bundle"
	A="VMware-Workstation-Full-${_basever}-${_build}.i386.bundle"
fi
source=($A vmware2.6.39.patch)

makedepends=(libxslt)
optdepends=("make" "gcc: For build kernel modules")

install=$pkgname.install

build() {

	# Loading buil function, change from Gentoo vmware-bundle.eclass
	source $startdir/build_function
	for i in vmware-player-app vmware-player-setup vmware-workstation vmware-vix; do
		vmware_bundle_extract_bundle_component "${srcdir}/${A}" $i ${srcdir}/$i
	done
}

package() {

	msg2 "Install vmware-player"
	mkdir -p ${pkgdir}${VM_INSTALL_DIR}/lib/vmware
	cp -af ${srcdir}/vmware-player-app/bin ${pkgdir}${VM_INSTALL_DIR}/
	cp -af ${srcdir}/vmware-player-app/sbin ${pkgdir}${VM_INSTALL_DIR}/
	cp -af ${srcdir}/vmware-player-app/lib/* ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/
	mkdir -p ${pkgdir}/usr
	cp -af ${srcdir}/vmware-player-app/share ${pkgdir}/usr/
	mkdir -p ${pkgdir}/usr/share/doc/vmware-player
	cp -af ${srcdir}/vmware-player-app/doc/* ${pkgdir}/usr/share/doc/vmware-player/
	install -Dm755 ${srcdir}/vmware-player-setup/vmware-config \
		${pkgdir}${VM_INSTALL_DIR}/lib/vmware/setup/vmware-config

	mkdir -p ${pkgdir}/etc
	cp -af ${srcdir}/vmware-player-app/etc/* ${pkgdir}/etc/
	rm -rf ${pkgdir}/etc/init.d

	msg2 "Install vmware-workstation"
	cp -af ${srcdir}/vmware-workstation/bin/* ${pkgdir}${VM_INSTALL_DIR}/bin/
	cp -af ${srcdir}/vmware-workstation/lib/* ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/
	cp -af ${srcdir}/vmware-workstation/share/* ${pkgdir}/usr/share/
	install -Dm755 ${srcdir}/vmware-workstation/man/man1/vmware.1.gz \
		${pkgdir}/usr/share/man/man1/vmware.1.gz
	mkdir -p ${pkgdir}/usr/share/doc/vmware-workstation
	cp -af ${srcdir}/vmware-workstation/doc/* ${pkgdir}/usr/share/doc/vmware-workstation/

	# Install vmware-vix
	cp -af ${srcdir}/vmware-vix/bin/* ${pkgdir}${VM_INSTALL_DIR}/bin/
	cp -af ${srcdir}/vmware-vix/lib ${pkgdir}${VM_INSTALL_DIR}/lib/vmware-vix
	ln -sf vmware-vix/libvixAllProducts.so ${pkgdir}${VM_INSTALL_DIR}/lib/libbvixAllProducts.so
	mkdir -p ${pkgdir}/usr/include/vmware-vix
	cp -af ${srcdir}/vmware-vix/include/* ${pkgdir}/usr/include/vmware-vix/
	mkdir -p ${pkgdir}/usr/share/doc/vmware-vix
	cp -af ${srcdir}/vmware-vix/doc/* ${pkgdir}/usr/share/doc/vmware-vix/

	msg2 "install thnuclnt"
	install -m755 ${srcdir}/vmware-player-app/thinprint/bin/thnuclnt \
		${pkgdir}${VM_INSTALL_DIR}/lib/vmware/bin/thnuclnt

	msg2 "delete the pre-compiled kernel modules"
	rm -rf ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/modules/binary

	# install patches for sources of VMware kernel modules
	install -m755 ${srcdir}/vmware2.6.39.patch ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/modules/source/vmware2.6.39.patch

	msg2 "delete unused vmware-tools-upgrader"
	if [ "${CARCH}" = 'x86_64' ]; then
		rm -f ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/tools-upgraders/{vmware-tools-upgrader-32,*.exe}
	else
		rm -f ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/tools-upgraders/{vmware-tools-upgrader-64,*.exe}
	fi

	# delete the VMware-tools
	#rm -f ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/isoimages/*.iso

	msg2 "create symlinks for the various tools"
	for tool in vmware vmplayer{,-daemon} \
			vmware-{acetool,gksu,fuseUI,modconfig{,-console},netcfg,tray,unity-helper} ; do
		ln -sf appLoader ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/bin/"${tool}"
	done
	rm -f ${pkgdir}${VM_INSTALL_DIR}/bin/{vmplayer,vmware}
	ln -sf ../lib/vmware/bin/vmplayer ${pkgdir}${VM_INSTALL_DIR}/bin/vmplayer
	ln -sf ../lib/vmware/bin/vmware ${pkgdir}${VM_INSTALL_DIR}/bin/vmware

	# fix up permissions
	chmod 04711 ${pkgdir}${VM_INSTALL_DIR}/bin/*
	chmod 0755  ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/{bin/*,lib/wrapper-gtk24.sh,setup/*}
	chmod 04711 ${pkgdir}${VM_INSTALL_DIR}/sbin/vmware-authd
	chmod 04711 ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/bin/vmware-vmx*
	chmod 0755 ${pkgdir}${VM_INSTALL_DIR}/lib/vmware-vix/setup/*

	msg2 "runtime environment scripts"
	mkdir -p ${pkgdir}/etc/profile.d
	cat > ${pkgdir}/etc/profile.d/vmware.sh <<-EOF
		export PATH="${PATH}:${VM_INSTALL_DIR}/bin:${VM_INSTALL_DIR}/sbin:${VM_INSTALL_DIR}/lib/vmware/bin"
		export ROOTPATH="${ROOTPATH}:${VM_INSTALL_DIR}/bin"
	EOF
	chmod 0755 ${pkgdir}/etc/profile.d/vmware.sh

	msg2 "create the configuration files"
	mkdir -p ${pkgdir}/etc/vmware
	cat > ${pkgdir}/etc/vmware/bootstrap <<-EOF
		VMWARE_INSTALLER="${VM_INSTALL_DIR}"
		BINDIR='${VM_INSTALL_DIR}/bin'
		LIBDIR='${VM_INSTALL_DIR}/lib'
	EOF

	cat > ${pkgdir}/etc/vmware/config <<-EOF
		bindir = "${VM_INSTALL_DIR}/bin"
		libdir = "${VM_INSTALL_DIR}/lib/vmware"
		initscriptdir = "/etc/rc.d"
		authd.fullpath = "${VM_INSTALL_DIR}/sbin/vmware-authd"
		gksu.rootMethod = "su"
		VMCI_CONFED = "yes"
		VMBLOCK_CONFED = "yes"
		VSOCK_CONFED = "yes"
		NETWORKING = "yes"
		player.product.version = "${major_minor_revision}"
		product.buildNumber = "${build}"
		product.name = "VMware Workstation"
		workstation.product.version = "${major_minor_revision}"
	EOF

	# Special for vmware-vix
	cat >> ${pkgdir}/etc/vmware/config <<-EOF
		vmware.fullpath = "${VM_INSTALL_DIR}/bin/vmware"
		vix.libdir = "${VM_INSTALL_DIR}/lib/vmware-vix"
		vix.config.version = "1"
	EOF

	msg2 "install the rc script"
	install -Dm644 ${startdir}/vmware ${pkgdir}/etc/rc.d/vmware
	chmod 0755 ${pkgdir}/etc/rc.d/vmware

	msg2 "Update installation location"
	sed -e "s:@@LIBCONF_DIR@@:${VM_INSTALL_DIR}/lib/vmware/libconf:g" \
		-i ${pkgdir}${VM_INSTALL_DIR}/lib/vmware/libconf/etc/{gtk-2.0/{gdk-pixbuf.loaders,gtk.immodules},pango/pango{.modules,rc}}
	sed -e "s:@@BINARY@@:${VM_INSTALL_DIR}/bin/vmware:g" \
		-i ${pkgdir}/usr/share/applications/vmware-workstation.desktop
	sed -e "s:@@BINARY@@:${VM_INSTALL_DIR}/bin/vmplayer:g" \
		-i ${pkgdir}/usr/share/applications/vmware-player.desktop
	sed -e "s:@@BINARY@@:${VM_INSTALL_DIR}/bin/vmware-netcfg:g" \
		-i ${pkgdir}/usr/share/applications/vmware-netcfg.desktop	
}